package com.angel.webapp.servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import com.angel.architecture.exceptions.NonBusinessException;
import com.angel.common.keyValue.KeyValueResult;
import com.angel.webapp.manager.ImportManager;

/**
 * @author William
 * @since 03/February/2009
 */
public class UploadImportFileServlet extends UploadServlet{

	private static final long serialVersionUID = -7427467996390884097L;
	private static Logger LOGGER = Logger.getLogger(UploadImportFileServlet.class);
	
	protected static final String REQ_USER_GENERATOR_NAME_PARAMETER = "userGeneratorName";
	
	protected static final String COLUMN_NAME_ALIAS_SEPARATOR = ",";

	@Override
	protected void doProcess(FileItem fileItem, KeyValueResult keyValueResult,
			HttpServletRequest req, HttpServletResponse res) {
		LOGGER.info("Processing upload import file [" + fileItem.getName() + "].");
		String userGeneratorName = keyValueResult.getResultFor(REQ_USER_GENERATOR_NAME_PARAMETER).toString();
		ImportManager importManager = new ImportManager();
		importManager.setUserGeneratorName(userGeneratorName);
		importManager.validate();
		importManager.doImport(fileItem);
	}
	
	@Override
	protected void doCheckParameters(KeyValueResult keyValueResult) {
		super.doCheckParameters(keyValueResult);
        boolean containsUserGeneratorName = keyValueResult.containsResultFor(REQ_USER_GENERATOR_NAME_PARAMETER);
        
        if(	!containsUserGeneratorName){
            String errorMessage = "Export process needs to be executed some parameters which are not present: \n" +
                    "Parameter Name: " + REQ_USER_GENERATOR_NAME_PARAMETER + " Included: " + containsUserGeneratorName + ".\n";
            throw new NonBusinessException(errorMessage);
        }
	}
}